package com.epidemic.project.system.schedule.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.epidemic.project.system.schedule.domain.SysSchedule;
import com.epidemic.project.system.schedule.service.ISysScheduleService;
import com.epidemic.framework.web.controller.BaseController;
import org.springframework.stereotype.Controller;
import com.epidemic.framework.web.domain.AjaxResult;
import com.epidemic.common.utils.poi.ExcelUtil;
import com.epidemic.framework.web.page.TableDataInfo;

/**
 * 课程Controller
 *
 * @author sys
 * @date 2023-02-01
 */
@Controller
@RequestMapping("/system/schedule")
public class SysScheduleController extends BaseController {
    private String prefix = "system/schedule";

    @Autowired
    private ISysScheduleService sysScheduleService;

    @GetMapping()
    public String schedule() {
        return prefix + "/schedule";
    }

    /**
     * 查询课程列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysSchedule sysSchedule) {
        startPage();
        List<SysSchedule> list = sysScheduleService.selectSysScheduleList(sysSchedule);
        return getDataTable(list);
    }

    /**
     * 查询课程列表
     */
    @PostMapping("/listAll")
    @ResponseBody
    public TableDataInfo listAll() {
        List<SysSchedule> sysSchedules = sysScheduleService.selectSysScheduleList(new SysSchedule());
        for (SysSchedule sysSchedule : sysSchedules) {
            sysSchedule.setMonth(sysSchedule.getMonth() - 1L);
        }
        return getDataTable(sysSchedules);
    }


    /**
     * 导出课程列表
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysSchedule sysSchedule) {
        List<SysSchedule> list = sysScheduleService.selectSysScheduleList(sysSchedule);
        ExcelUtil<SysSchedule> util = new ExcelUtil<SysSchedule>(SysSchedule.class);
        return util.exportExcel(list, "schedule");
    }

    /**
     * 新增课程
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存课程
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysSchedule sysSchedule) {
        String totalTime = sysSchedule.getTotalTime();
        String[] s = totalTime.split(" ");
        String[] split = s[0].split(":");
        Long year = Long.valueOf(split[0]);
        Long month = Long.valueOf(split[1]);
        Long day = Long.valueOf(split[2]);
        String[] split1 = s[1].split(":");
        Long hour = Long.valueOf(split1[0]);
        Long points = Long.valueOf(split1[1]);
        if (points < 10L) {
            String replace = points.toString().replace("0", "");
            points = Long.valueOf(replace);
        }
        sysSchedule.setYear(year);
        sysSchedule.setMonth(month);
        sysSchedule.setDay(day);
        sysSchedule.setHour(hour);
        sysSchedule.setPoints(points);
        return toAjax(sysScheduleService.insertSysSchedule(sysSchedule));
    }

    /**
     * 修改课程
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SysSchedule sysSchedule = sysScheduleService.selectSysScheduleById(id);
        mmap.put("sysSchedule", sysSchedule);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysSchedule sysSchedule) {
        String totalTime = sysSchedule.getTotalTime();
        String[] s = totalTime.split(" ");
        String[] split = s[0].split(":");
        Long year = Long.valueOf(split[0]);
        Long month = Long.valueOf(split[1]);
        Long day = Long.valueOf(split[2]);
        String[] split1 = s[1].split(":");
        Long hour = Long.valueOf(split1[0]);
        Long points = Long.valueOf(split1[1]);
        if (points < 10L) {
            String replace = points.toString().replace("0", "");
            points = Long.valueOf(replace);
        }
        sysSchedule.setYear(year);
        sysSchedule.setMonth(month);
        sysSchedule.setDay(day);
        sysSchedule.setHour(hour);
        sysSchedule.setPoints(points);
        return toAjax(sysScheduleService.updateSysSchedule(sysSchedule));
    }

    /**
     * 删除课程
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(sysScheduleService.deleteSysScheduleByIds(ids));
    }
}
